package yc.hss.hss.controller;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.util.HtmlUtils;
import yc.hss.hss.vo.Greeting;
import yc.hss.hss.vo.HelloMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class WebSocketController {
    public static List<Map<String, Object>> GLOBAL_SEAT_DATA = null;
    public static Map<String, Map<String, Object>> GLOBAL_INDEX_DATA = null;
    public static Map<String, Map<String, Object>> GLOBAL_UUID_DATA = null;


    @MessageMapping("/login")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        return new Greeting(true, getGlobalSeatData(), message);
    }

    @MessageMapping("/lockSeat")
    @SendTo("/topic/greetings")
    public Greeting lockSeat(HelloMessage message) throws Exception {
        return new Greeting(setGlobalSeatData(message), getGlobalSeatData(), message);
    }

    @GetMapping("/index-admin")
    public String indexAdmin() throws Exception {
        return "index-admin";
    }

    @GetMapping("/index")
    public String index() throws Exception {
        return "index";
    }

    public static List<Map<String, Object>> getGlobalSeatData() {
        if (GLOBAL_SEAT_DATA == null) {
            GLOBAL_SEAT_DATA = new ArrayList<>();
            GLOBAL_INDEX_DATA = new HashMap<>();
            GLOBAL_UUID_DATA = new HashMap<>();
            String[] seatNo = new String[]{"A","B","C", "D", "E", "F"};
            for(int i = 0; i < 40; i ++){
                Map<String, Object> row = new HashMap<>();
                row.put("rowIndex", i + 1);
                List<Map<String, Object>> left = new ArrayList<>();
                List<Map<String, Object>> right = new ArrayList<>();
                row.put("left", left);
                row.put("right", right);
                GLOBAL_SEAT_DATA.add(row);
                for(int j = 0;j < 6; j ++){
                    String index = row.get("rowIndex") + seatNo[j];
                    Map<String, Object> cell = new HashMap<>();
                    cell.put("index", index);
                    cell.put("label", "");
                    cell.put("icon", "imgs/empty.png");
                    GLOBAL_INDEX_DATA.put(index, cell);
                    if(j < 3){
                        left.add(cell);
                    }else{
                        right.add(cell);
                    }
                }
            }
        }
        return GLOBAL_SEAT_DATA;
    }

    public synchronized static boolean setGlobalSeatData(HelloMessage message) {
        Map<String, Object> cell = GLOBAL_INDEX_DATA.get(message.getIndex());
        // 空座位，更新座位状态
        if(cell.get("icon").equals("imgs/empty.png")){
            Map<String, Object> oldcell = GLOBAL_UUID_DATA.get(message.getUuid());
            if(oldcell!= null){
                oldcell.put("icon", "imgs/empty.png");
                oldcell.put("uuid", "");
                oldcell.put("label", "");
            }
            cell.put("icon", "imgs/used.png");
            cell.put("uuid", message.getUuid());
            cell.put("label","【" + message.getName() + "】");
            GLOBAL_UUID_DATA.put(message.getUuid(), cell);
            return true;
        }else{
            // 本人操作，释放座位
            if(cell.get("uuid").equals(message.getUuid())){
                cell.put("icon", "imgs/empty.png");
                cell.put("uuid", "");
                cell.put("label", "");
                return true;
            }
        }
        return false;
    }


}
